package com.sun.controller;

import com.sun.dao.DepartmentDao;
import com.sun.dao.EmployeeDao;
import com.sun.entities.Department;
import com.sun.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author sun
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;
    /**
     * 查询所有员工
     * @return 所有员工的集合
     */
    @RequestMapping("/emps")
    public String getEmps(Model model){
        Collection<Employee> all=employeeDao.getAll();
        model.addAttribute("emps",all);
        return "/list";
    }

    /**
     * 去员工添加页面
     * @return 员工添加页面jsp
     */
    @RequestMapping(value = "/toAddPage")
    public String toAddPage(Model model){
        //第一步查出所有部门
        Collection<Department> departments = departmentDao.getDepartments();
        //放到请求域中
        model.addAttribute("depts", departments);
        model.addAttribute("employee",new Employee());
        //转发到添加页面
        return "/add";
    }
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public String addEmp(@Valid Employee employee, BindingResult result){
        System.out.println("要添加的员工是"+employee);
        //获取是否有校验错误
        boolean hasErrors = result.hasErrors();
        if(hasErrors){
            //输出错误信息
            System.out.println("校验错误");
            List<FieldError> errors = result.getFieldErrors();
            for(FieldError error:errors){
                System.out.println("错误字段："+error.getField());
                System.out.println("错误消息：" +error.getDefaultMessage());
                System.out.println(error);
                System.out.println("-----------------------");
            }
            //校验错误返回原来的页面
            return "/add";
        }
        employeeDao.save(employee);

        //返回列表页面，重定向到查询所有员工的请求
        return "redirect:/emps";
    }

    /**
     * 根据id获取员工信息，编辑页面
     * @param id
     * @return 编辑页面
     */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public String getEmp(@PathVariable("id") Integer id,Model model){
        //根据id查询employee
        Employee employee = employeeDao.get(id);
        model.addAttribute("employee", employee);

        return "/edit";
    }

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    public String updateEmp(@ModelAttribute("employee") Employee employee){
        System.out.println("要修改的员工："+ employee);
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    public String deleteEmp(@PathVariable("id")Integer id, Employee employee){
        System.out.println("删除的员工是"+employee);
        employeeDao.delete(id);
        return "redirect:/emps";
    }

    @ModelAttribute
    public void getModelEmp(@RequestParam(value = "id",required = false) Integer id,Model model){
        if(id!=null){
            Employee employee = employeeDao.get(id);
            model.addAttribute("employee", employee);
        }
        //第一步查出所有部门
        Collection<Department> departments = departmentDao.getDepartments();
        //放到请求域中
        model.addAttribute("depts", departments);
        System.out.println("att");
    }
}
